<?

class Mdl_Visitors extends MY_Model {

    function Mdl_Visitors() {

        parent::MY_Model();

        $this->limit = 25;

    }

    function get($params = NULL) {

        $this->prep_params($params);

        if (!isset($params['order_by'])) {

            $this->db->order_by('date DESC');

        }

        $offset = ($this->page * $this->limit) - $this->limit;

        $this->db->limit($this->limit, $offset);

        $query = $this->db->get('inktype_visitors');

        $this->set_page_links();

        return $query->result();

    }

    function get_summary($params = NULL) {

        $this->db->select("DATE_FORMAT(FROM_UNIXTIME(date), '%m-%d-%Y') AS g_date, COUNT(*) AS num_total, (SELECT COUNT(DISTINCT ip_address) FROM inktype_visitors WHERE DATE_FORMAT(FROM_UNIXTIME(date), '%m-%d-%Y') = g_date) AS num_unique", FALSE);

        $this->db->order_by('date DESC');

        $this->db->group_by('g_date');

        $offset = ($this->page * $this->limit) - $this->limit;

        $this->db->limit($this->limit, $offset);

        $query = $this->db->get('inktype_visitors');

        $this->set_page_links();

        return $query->result();

    }

    function save() {

        $this->load->library('user_agent');

        if ($this->agent->is_browser()) {

            $db_array = array(
                'date'          =>  time(),
                'browser'       =>  $this->agent->browser() . ' ' . $this->agent->version(),
                'platform'      =>  $this->agent->platform(),
                'referrer'      =>  $this->agent->referrer(),
                'current_url'   =>  current_url(),
                'ip_address'    =>  $this->input->ip_address()
            );

            $this->db->insert('inktype_visitors', $db_array);

        }

    }

}

?>